/*
 * QuestionAddDialog.java
 *
 * Created on 11 February 2009, 12:16
 */

package bluetoothserver.graphical;

import bluetoothserver.dataStore.Question;
import bluetoothserver.dataStore.QuestionCollection;
/**
 * This Dialog box will appear when a user requests to add a new question
 * @version 1.0 FINAL
 * @author  Philip White
 */
public class QuestionAddDialog extends javax.swing.JDialog {
    private String title;
    private int type;
    private String[] answers;
    private boolean completed;
    private int correct;
    private Question q;
    
    /** Creates new form QuestionAddDialog */
    public QuestionAddDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        completed = false;
        this.setTitle("Add Question");
        this.setLocation(250, 250);
        this.setVisible(true);
        

    }
    /**
     * This method is called once the user has finished writing the question and converts the data into a question to be added to the database
     */
    private void collectData(){
        title = qName.getText();
        type = typeCombo.getSelectedIndex();
        answers = new String[6];
        answers[0] = jTextField2.getText();
        answers[1] = jTextField3.getText();
        answers[2] = jTextField4.getText();
        answers[3] = jTextField5.getText();
        answers[4] = jTextField6.getText();
        answers[5] = jTextField7.getText();
        if(type == QuestionCollection.MULTIMARK){
            correct = multiCorrect();
        }else{
            correct = getCorrect();
        }
        q = new Question(title,type,answers,correct);
        
    }

    /**
     * This method collects the correct answer from the dialog
     * @return int value that is the answer
     */
    private int getCorrect(){
        int i = 0;
        if(jCheckBox1.isSelected()){
            i = 1;
        }else if(jCheckBox2.isSelected()){
            i = 2;
        }else if(jCheckBox3.isSelected()){
            i = 3;
        }else if(jCheckBox4.isSelected()){
            i = 4;
        }else if(jCheckBox5.isSelected()){
            i = 5;
        }else if(jCheckBox6.isSelected()){
            i = 6;
        }
        return i;
    }

    private int multiCorrect(){
        String s = "0";
        if(jCheckBox1.isSelected()){
            s = s+1;
        }
        if(jCheckBox2.isSelected()){
            s = s+2;
        }
        if(jCheckBox3.isSelected()){
            s = s+3;
        }
        if(jCheckBox4.isSelected()){
            s = s+4;
        }
        if(jCheckBox5.isSelected()){
            s = s+5;
        }
        if(jCheckBox6.isSelected()){
            s = s+6;
        }
        return Integer.parseInt(s);
    }

    /**
     * This method returns the question to the parent frame
     * @return the question that was just written
     */
    public Question getQuestion(){
        return q;
    }

    /**
     * This informs the parent frame that the user wants the question to be added to the list
     * @return true or false depending on which button was pressed
     */
    public boolean isCompleted(){
        return completed;
    }

    /**
     * This method changes the appearence of the answer windows depending on which question type is selected
     * @param type the question type
     */
    private void answerSettings(int type){
        this.clearSelected();
        if(type == QuestionCollection.YES_NO){
            this.changeState(false);
            this.changeText("Yes","No","","","","");
            this.changeSelectable(true,true,false,false,false,false);
        }else if(type == QuestionCollection.TRUE_FALSE){
            this.changeState(false);
            this.changeText("True","False","","","","");
            this.changeSelectable(true,true,false,false,false,false);
        }else if(type == QuestionCollection.SEQUENCE){
            this.changeState(true);
            this.changeText("", "", "", "", "", "");
            this.changeSelectable(false,false,false,false,false,false);
        }else if(type == QuestionCollection.RATING){
            this.changeState(false);
            this.changeText("", "", "", "", "", "");
            this.changeSelectable(false,false,false,false,false,false);
        }else if(type == QuestionCollection.TEXT_ENTRY){
            this.changeState(false);
            this.changeText("", "", "", "", "", "");
            this.changeSelectable(false,false,false,false,false,false);
        }else if(type == QuestionCollection.VOTING){
            this.changeState(true);
            this.changeText("", "", "", "", "", "");
            this.changeSelectable(false,false,false,false,false,false);
        }else{
            this.changeState(true);
            this.changeText("", "", "", "", "", "");
            this.changeSelectable(true,true,true,true,true,true);
        }
    }
    /**
     * this will make all or none of the text fields editable
     * @param state selected or not
     */
    private void changeState(boolean state){
        jTextField2.setEditable(state);
        jTextField3.setEditable(state);
        jTextField4.setEditable(state);
        jTextField5.setEditable(state);
        jTextField6.setEditable(state);
        jTextField7.setEditable(state);
    }
    /**
     * This sets the text that will appear in the text fields
     * @param a text field 1
     * @param b text field 2
     * @param c text field 3
     * @param d text field 4
     * @param e text field 5
     * @param f text field 6
     */
    private void changeText(String a,String b,String c,String d,String e,String f){
        jTextField2.setText(a);
        jTextField3.setText(b);
        jTextField4.setText(c);
        jTextField5.setText(d);
        jTextField6.setText(e);
        jTextField7.setText(f);
    }

    /**
     * This makes certain check boxes enable depending on the parameters entered
     * @param a check box 1
     * @param b check box 2
     * @param c check box 3
     * @param d check box 4
     * @param e check box 5
     * @param f check box 6
     */
    private void changeSelectable(boolean a,boolean b,boolean c,boolean d,boolean e, boolean f){
        jCheckBox1.setEnabled(a);
        jCheckBox2.setEnabled(b);
        jCheckBox3.setEnabled(c);
        jCheckBox4.setEnabled(d);
        jCheckBox5.setEnabled(e);
        jCheckBox6.setEnabled(f);
        
    }
    /**
     * this clears all of the check boxes usually run when the type is changed
     */
    private void clearSelected(){
        jCheckBox1.setSelected(false);
        jCheckBox2.setSelected(false);
        jCheckBox3.setSelected(false);
        jCheckBox4.setSelected(false);
        jCheckBox5.setSelected(false);
        jCheckBox6.setSelected(false);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        qName = new javax.swing.JTextField();
        qTitle = new javax.swing.JLabel();
        typeCombo = new javax.swing.JComboBox();
        qType = new javax.swing.JLabel();
        jCheckBox1 = new javax.swing.JCheckBox();
        jTextField2 = new javax.swing.JTextField();
        aTitle = new javax.swing.JLabel();
        jCheckBox2 = new javax.swing.JCheckBox();
        jTextField3 = new javax.swing.JTextField();
        jCheckBox3 = new javax.swing.JCheckBox();
        jTextField4 = new javax.swing.JTextField();
        jCheckBox4 = new javax.swing.JCheckBox();
        jTextField5 = new javax.swing.JTextField();
        jCheckBox5 = new javax.swing.JCheckBox();
        jTextField6 = new javax.swing.JTextField();
        jCheckBox6 = new javax.swing.JCheckBox();
        jTextField7 = new javax.swing.JTextField();
        addQ = new javax.swing.JButton();
        cancel = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });
        getContentPane().setLayout(new java.awt.GridBagLayout());
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(4, 10, 4, 10);
        getContentPane().add(qName, gridBagConstraints);

        qTitle.setText("Question Title");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(10, 10, 0, 0);
        getContentPane().add(qTitle, gridBagConstraints);

        typeCombo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Multichoice", "Numeric", "Yes / No", "True / False", "Sequence", "Multiple Mark", "Voting", "Rating Scale", "Text Entry" }));
        typeCombo.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                typeComboItemStateChanged(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(4, 10, 4, 0);
        getContentPane().add(typeCombo, gridBagConstraints);

        qType.setText("Question Type");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 10, 0, 0);
        getContentPane().add(qType, gridBagConstraints);

        jCheckBox1.setText("1)");
        jCheckBox1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                jCheckBox1MousePressed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(0, 10, 0, 0);
        getContentPane().add(jCheckBox1, gridBagConstraints);

        jTextField2.setMaximumSize(new java.awt.Dimension(50, 27));
        jTextField2.setMinimumSize(new java.awt.Dimension(50, 27));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.gridwidth = java.awt.GridBagConstraints.RELATIVE;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.ipadx = 260;
        gridBagConstraints.ipady = 3;
        gridBagConstraints.insets = new java.awt.Insets(4, 0, 4, 10);
        getContentPane().add(jTextField2, gridBagConstraints);

        aTitle.setText("Answers");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 10, 0, 0);
        getContentPane().add(aTitle, gridBagConstraints);

        jCheckBox2.setText("2)");
        jCheckBox2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                jCheckBox2MousePressed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 6;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        getContentPane().add(jCheckBox2, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 6;
        gridBagConstraints.gridwidth = java.awt.GridBagConstraints.RELATIVE;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.ipady = 3;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 10);
        getContentPane().add(jTextField3, gridBagConstraints);

        jCheckBox3.setText("3)");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 7;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        getContentPane().add(jCheckBox3, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 7;
        gridBagConstraints.gridwidth = java.awt.GridBagConstraints.RELATIVE;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.ipady = 3;
        gridBagConstraints.insets = new java.awt.Insets(4, 0, 4, 10);
        getContentPane().add(jTextField4, gridBagConstraints);

        jCheckBox4.setText("4)");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 8;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        getContentPane().add(jCheckBox4, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 8;
        gridBagConstraints.gridwidth = java.awt.GridBagConstraints.RELATIVE;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.ipady = 3;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 10);
        getContentPane().add(jTextField5, gridBagConstraints);

        jCheckBox5.setText("5)");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 9;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        getContentPane().add(jCheckBox5, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 9;
        gridBagConstraints.gridwidth = java.awt.GridBagConstraints.RELATIVE;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.ipady = 3;
        gridBagConstraints.insets = new java.awt.Insets(4, 0, 4, 10);
        getContentPane().add(jTextField6, gridBagConstraints);

        jCheckBox6.setText("6)");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 10;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        getContentPane().add(jCheckBox6, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 10;
        gridBagConstraints.gridwidth = java.awt.GridBagConstraints.RELATIVE;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.ipady = 3;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 10);
        getContentPane().add(jTextField7, gridBagConstraints);

        addQ.setText("Add Question");
        addQ.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                addQMousePressed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 11;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(4, 0, 10, 10);
        getContentPane().add(addQ, gridBagConstraints);

        cancel.setText("Cancel");
        cancel.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                cancelMousePressed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 11;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(4, 10, 10, 0);
        getContentPane().add(cancel, gridBagConstraints);

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void addQMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_addQMousePressed
    completed = true;
    collectData();
    this.dispose();
}//GEN-LAST:event_addQMousePressed

private void cancelMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_cancelMousePressed
    this.dispose();
}//GEN-LAST:event_cancelMousePressed

private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
}//GEN-LAST:event_formWindowOpened

private void typeComboItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_typeComboItemStateChanged
    int i = typeCombo.getSelectedIndex();
    answerSettings(i);
}//GEN-LAST:event_typeComboItemStateChanged

private void jCheckBox1MousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jCheckBox1MousePressed

}//GEN-LAST:event_jCheckBox1MousePressed

private void jCheckBox2MousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jCheckBox2MousePressed

}//GEN-LAST:event_jCheckBox2MousePressed
  
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel aTitle;
    private javax.swing.JButton addQ;
    private javax.swing.JButton cancel;
    private javax.swing.JCheckBox jCheckBox1;
    private javax.swing.JCheckBox jCheckBox2;
    private javax.swing.JCheckBox jCheckBox3;
    private javax.swing.JCheckBox jCheckBox4;
    private javax.swing.JCheckBox jCheckBox5;
    private javax.swing.JCheckBox jCheckBox6;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    private javax.swing.JTextField jTextField4;
    private javax.swing.JTextField jTextField5;
    private javax.swing.JTextField jTextField6;
    private javax.swing.JTextField jTextField7;
    private javax.swing.JTextField qName;
    private javax.swing.JLabel qTitle;
    private javax.swing.JLabel qType;
    private javax.swing.JComboBox typeCombo;
    // End of variables declaration//GEN-END:variables
    
}
